package cn.yanhu.kuwanapp.utils

import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import cn.woochen.comlib.adapter.OnItemClickListener
import cn.woochen.comlib.dialog.AlertDialog
import cn.woochen.comlib.util.ClipboardUtil
import cn.woochen.comlib.util.ConfigPref
import cn.woochen.comlib.util.ext.setOnSingleClickListener
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.bean.other.ShareBean
import cn.yanhu.kuwanapp.common.activity.CommonWebviewActivity
import cn.yanhu.kuwanapp.invite.adapter.ShareAdapter
import cn.yanhu.kuwanapp.utils.constans.Constants
import cn.yanhu.kuwanapp.utils.ext.showToast
import cn.yanhu.kuwanapp.utils.third.AccountShareType
import cn.yanhu.kuwanapp.utils.third.ThirdAccountHelper

/**
 * 通用确弹窗
 * @author woochen
 * @time 2018/8/30 9:57
 */

object SimpleDialogUtil {

    /**
     * 显示弹窗
     * @see R.layout.dialog_exit_account //退出登录
     * @see R.layout.dialog_cancel_account //注销账户
     */
    fun create(
        context: Context, @LayoutRes layoutRes: Int = R.layout.dialog_common, dismissListener: DialogInterface.OnDismissListener? = null, canceledOnTouchOutside: Boolean = true, fromBottom: Boolean = false, styleAnimation: Int = 0, windowWidth: Int = 0, windowHeight: Int = 0
    ): AlertDialog? {
        if (context is Activity && !context.isFinishing) {
            val builder = AlertDialog.Builder(context).setContentView(layoutRes)
            if (fromBottom) builder.fromBottom(fromBottom)
            if (windowWidth != 0 && windowHeight != 0) builder.setWidthAndHeight(
                windowWidth, windowHeight
            )
            if (styleAnimation > 0) builder.addAnimation(styleAnimation)
            val dialog = builder.create()
            if (dismissListener != null) dialog.setOnDismissListener(dismissListener)
            dialog.setCancelable(canceledOnTouchOutside)
            dialog.setCanceledOnTouchOutside(canceledOnTouchOutside)
            return dialog
        }
        return null
    }


    fun share(context: Context, inviteUrl: String?) {
        create(context, layoutRes = R.layout.dialog_share, fromBottom = true)?.apply {
            val rvItem = getView<RecyclerView>(R.id.rv_item)
            val datas = listOf<ShareBean>(
                ShareBean(1, "朋友圈", R.drawable.ic_wx_circle),
                ShareBean(2, "微信好友", R.drawable.ic_wx_friend),
                ShareBean(3, "QQ好友", R.drawable.ic_qq),
                ShareBean(4, "复制链接", R.drawable.ic_link)
            )
            val shareAdapter = ShareAdapter(datas)
            rvItem?.adapter = shareAdapter
            shareAdapter.mItemClickListener = OnItemClickListener {
                val shareBean = datas[it]
                when (shareBean.type) {
                    1 -> {
                        ThirdAccountHelper.sharePageLink(
                            context,
                            url = inviteUrl ?: "",
                            shareChannel = AccountShareType.WX_CIRCLE
                        )
                    }
                    2 -> {
                        ThirdAccountHelper.sharePageLink(
                            context,
                            url = inviteUrl ?: "",
                            shareChannel = AccountShareType.WX_SESSION
                        )
                    }
                    3 -> {
                        ThirdAccountHelper.sharePageLink(
                            context, url = inviteUrl ?: "", shareChannel = AccountShareType.QQ
                        )
                    }
                    4 -> {
                        ClipboardUtil.copyToClipboard(context, inviteUrl ?: "") {
                            showToast("链接已复制")
                        }
                    }
                }
                dismiss()
            }
        }?.show()
    }

    private var isAgreePrivacy by ConfigPref(Constants.IS_AGREE_PRIVACY, false)

    fun privacyDialog(context: Context,disAgree:()->Unit,agree:()->Unit) {
        val str =
            "感谢您使用酷爱玩App！\n我们非常重视您的个人信息和隐私保护，为了更好地保障您的个人权益，在您使用我们的产品前，请务必审慎阅读《酷爱玩用户协议》与《隐私政策》内的所有条款，尤其是：\n1.我们对您的个人信息的收集/保存/使用/对外提供/保护等规则条款，以及您的用户权利等条款；\n2.约定我们的限制责任，免责条款。\n您点击“同意”的行为即表示您已阅读完毕并同意以上协议的全部内容。"
        val spannableString1 = SpannableString(str)
        val a = str.indexOf("《酷爱玩用户协议》")
        val b = str.indexOf("《隐私政策》")
        spannableString1.setSpan(
            ForegroundColorSpan(Color.parseColor("#FF6E28")), a, a + 9, Spanned.SPAN_EXCLUSIVE_INCLUSIVE
        )
        spannableString1.setSpan(
            ForegroundColorSpan(Color.parseColor("#FF6E28")), b, b + 6, Spanned.SPAN_EXCLUSIVE_INCLUSIVE
        )
        setSpanClick(context,spannableString1, a, 9, Config.USER_AGREEMENT)
        setSpanClick(context,spannableString1, b, 6, Config.USER_PRIVATE_AGREEMENT)


        create(context, layoutRes = R.layout.dialog_privacy)?.apply {
            val tvContent = getView<TextView>(R.id.tv_content)
            val tvDisagree = getView<TextView>(R.id.tv_disagree)
            val tvAgree = getView<TextView>(R.id.tv_agree)
            tvContent?.let {
                it.highlightColor = context.resources.getColor(R.color.transparent)
                it.movementMethod = LinkMovementMethod.getInstance()
                it.text = spannableString1
            }
            tvDisagree?.setOnSingleClickListener {
                disAgree.invoke()
                dismiss()
            }
            tvAgree?.setOnSingleClickListener {
                isAgreePrivacy = true
                agree.invoke()
                dismiss()
            }
        }?.show()
    }

    private fun setSpanClick(context: Context,spannableString:SpannableString,  start:Int,  length:Int,  url:String) {
        spannableString.setSpan(object : ClickableSpan() {

            override fun updateDrawState(ds: TextPaint) {
                ds.isUnderlineText = false
            }

            override fun onClick(widget: View) {
                CommonWebviewActivity.start(context,url,true)
            }
        }, start, start + length, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
    }

}
